package lnp;

import static org.junit.Assert.assertTrue;
import lnp.test.RobotTestBed;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;


/**
 * Illustrates JUnit testing of Robocode robots.
 * This test simply verifies that FiringDock moves 
 * randomly after each firing.
 * 
 * @author Lyneth Peou
 *
 */
public class TestFiringDockMoveEachFire extends RobotTestBed {
  /** True if the robot moves to new position each firing. */
  Boolean movesEachFire = false;
  /** True if the robot moves to new position. */ 
  Boolean newPosition = false;
  /** Initialize the current robot position. */
  double currentX = 0.0;
  double currentY = 0.0;   
  /** Initialize the previous position. */
  double previousX = 0.0;
  double previousY = 0.0;

  /* Initialize the current and previous energy. */
  double previousEnergy = 0.0;
  double currentEnergy = 0.0;   


  /**
   * Specifies that Crazy and FiringDock are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Crazy,lnp.FiringDock";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  } 

  /**
   * Assuming that the robot fire a bullet if the energy drop between 0.1 to 3  
   * Tracking the movement of robot after each bullet fires. 
   * 
   *  @param e details about the TurnEndedEvent
   */
  
  @Override
  public void onTurnEnded(TurnEndedEvent e) {
    IRobotSnapshot r = e.getTurnSnapshot().getRobots()[1];    
    int turn = e.getTurnSnapshot().getTurn();    
   
    //Set initial energy, inital position
    if (turn == 0) {
      currentX = r.getX();
      currentY = r.getY();
      currentEnergy = r.getEnergy();
    }
    else {
      previousX = currentX; 
      previousY = currentY;
      
      currentX = r.getX();
      currentY = r.getY();
      
      previousEnergy = currentEnergy;
      currentEnergy = r.getEnergy();
    }
    if (previousX != currentX || previousY != currentY) {
      newPosition = true; 
    }
    
    double BulletPower = previousEnergy - currentEnergy;
    
    if ((BulletPower <= 3 || BulletPower >= 0.1) && newPosition) {
      movesEachFire = true; 
    }
   }
  
  /**
   * Asserts that FiringDock moves at least 5 times.
   * @param e Details about the completed round.
   */
  public void onRoundEnded(RoundEndedEvent e) {    
     assertTrue("Check FiringDock movement", movesEachFire );   
  }  
  
}
